package com.smartview.jboss.spring.mvc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.smartview.jboss.spring.dto.LoginDTO;
import com.smartview.jboss.spring.dto.NewsDTO;
import com.smartview.jboss.spring.dto.UserDTO;
import com.smartview.jboss.spring.dto.WeatherDTO;
import com.smartview.jboss.spring.dao.LoginDao;
import com.smartview.jboss.spring.dao.WeatherDao;
import com.smartview.jboss.spring.model.DataFormatter;
import com.smartview.jboss.spring.model.DataParser;
import com.smartview.jboss.spring.model.ShoppingCart;
import com.smartview.jboss.spring.model.YahooData;

@Controller
public class NewsService {
	@Autowired
	private YahooData data;
	@Autowired
	private DataParser parser;
	@Autowired
	private DataFormatter formatter;
	@Autowired
	private LoginDao loginDao;

	
//	@RequestMapping(method = RequestMethod.GET)
//	public @ModelAttribute("message") String readWeather() {
//		return "please enter a zip code";
//	}
//	 

	@RequestMapping(value = "/showNews", method = RequestMethod.POST)
	public @ModelAttribute("newsInfo") ModelAndView readNews(@RequestParam("key") String key) throws DocumentException, IOException {
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("showNews");
		
			ArrayList<NewsDTO> a = parser.parseNews(key ,data.retrieveNews());
			
			// Add this list to this dynamic web page "getWeather.jsp". Name this list "weatherList"
			mav.addObject("newsList", a);
			
			return mav;
	}
	
	/**
	 *  Most of login process will be handled by this method
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * @author yj
	 */
	@RequestMapping("/news/main")
	public ModelAndView loginProc(HttpServletRequest request, HttpServletResponse response) throws Exception{
		// SetView
		ModelAndView mav = new ModelAndView("news/main");
		
		// GetParameter
		String id = SecurityContextHolder.getContext().getAuthentication().getName();
		List<LoginDTO> list = loginDao.getServiceAccounts(id);
		
		String useYahooWeather = "0";
		String useYahooNews = "0";
		String useYahooCompany = "0";
		
		for (LoginDTO dto : list) {
			if(dto.getService_code().equals("0")) {
				useYahooWeather = "1";
				WeatherDTO weather = parser.parseWeather(dto.getService_id() ,data.retrieveWeather(dto.getService_id()));
				
				mav.addObject("weather", weather);
			}
			
			if(dto.getService_code().equals("3")) {
				useYahooNews = "1";
				ArrayList<NewsDTO> news = parser.parseNews("" ,data.retrieveNews());
				
				mav.addObject("newsList", news);
			}
			
			if(dto.getService_code().equals("4")) {
				useYahooCompany = "1";
				ArrayList<NewsDTO> company = parser.parseCompany(dto.getService_id() ,data.retrieveCompany(dto.getService_id()));
				
				mav.addObject("companyList", company);
			}

				
		}
		
		mav.addObject("id", id);
		mav.addObject("use0", useYahooWeather);
		mav.addObject("use3", useYahooNews);
		mav.addObject("use4", useYahooCompany);
		
		return mav;
	}

}

















